#include <dde_kit/printf.h>

#include <eku/stdio.h>

void
dde_kit_print(const char *msg)
{
	 printf("%s", msg);
}

void
dde_kit_printf(const char *fmt, ...)
{
	 va_list ap;
	 va_start(ap, fmt);
	 vprintf(fmt, ap);
	 printf("\n");
	 va_end(ap);
}

void
dde_kit_vprintf(const char *fmt, va_list va)
{
	 vprintf(fmt, va);
}

#include <eku/proc.h>

void
dde_kit_print_cur_proc(void)
{
	 printf("[pid %d]\n", cur_pid());
}

void
dde_kit_print_backtrace(void)
{
	 uint32_t eip;
	 uint32_t ebp, nebp;
	 asm volatile("movl %%ebp, %0" : "=r" (ebp));

     printf("Stack backtrace:\n");
     while (1)
     {
		  asm volatile("movl 4(%0), %1" : "=r" (eip) : "r"(ebp));
		  asm volatile("movl (%0), %1"
					   : "=r" (nebp)
					   : "q" (ebp)
			   );
		  printf("  ebp: %08x  eip: %08x\n", ebp, eip);
		  if ((ebp = nebp) == 0) break;
	 }
}

#include <eku/symbol.h>

void
dde_kit_printf_symbol(void)
{
	 export_symbol("dde_kit_print", &dde_kit_print, 1);
	 export_symbol("dde_kit_printf", &dde_kit_printf, 1);
	 export_symbol("dde_kit_vprintf", &dde_kit_vprintf, 1);
	 export_symbol("dde_kit_print_cur_proc", &dde_kit_print_cur_proc, 1);
	 export_symbol("dde_kit_print_backtrace", &dde_kit_print_backtrace, 1);
}
